package com.lomboardrisk.app.ee_new.web.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.lomboardrisk.app.ee_new.service.ColworkflowService;
import com.lomboardrisk.app.ee_new.web.AJAXTransferBean;
import com.lomboardrisk.app.ee_new.web.WebGuiResponseUtils;
import com.lomboardrisk.app.ee_new.web.displayItem.JqGridData;
import com.lomboardrisk.app.ee_new.web.displayItem.JqGridItem;
import com.lomboardrisk.app.ee_new.web.jsonUtils.DateJsonValueProcessor;
import com.lomboardrisk.app.ee_new.web.model.Colworkflow;


public class JGridActionByHibernate extends DispatchAction {

	private ColworkflowService workflowService;
	
	public void setWorkflowService(ColworkflowService workflowService) {
		this.workflowService = workflowService;
	}

	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		
		return null;
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// XStree Test Start
//		XStream xstream = new XStream();
//		xstream.alias("nav", Nav.class);
//		xstream.alias("header", Header.class);
//		xstream.alias("item", Item.class);
//		xstream.alias("condition", Condition.class);
//		xstream.setMode(XStream.NO_REFERENCES);
//		
//		BufferedReader is = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("ee.cfg.xml")));
//		
//		StringBuffer buffer = new StringBuffer();
//		String str;
//		while((str = is.readLine()) != null) {
//			buffer.append(str);
//		}
		
//		String xml = xstream.toXML(joe);
		
//		System.out.println(xml);
//		Nav newJoe = (Nav)xstream.fromXML(buffer.toString());
		
		// XStree end 
		
		long page = Long.parseLong(request.getParameter("page"));
		long pageSize = Long.parseLong(request.getParameter("rows"));
		
		List<Colworkflow> workflows = workflowService.findById(0);
		List<JqGridItem> items = new ArrayList<JqGridItem>();
		JqGridItem item;
		for(int i=0; i<workflows.size(); i++) {
			item = new JqGridItem();
			BeanUtils.copyProperties(item, workflows.get(i));
			items.add(item);
		}
		
        long totalrecords = 0;
        JqGridData data = new JqGridData();
		data.setItems(items);
		data.setTotal(totalrecords/pageSize);
		data.setPage(page);
		data.setRecords(totalrecords);
		JsonConfig jsonConfig = new JsonConfig();
//		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsDateJsonValueProcessor());
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		
		JSONObject result = JSONObject.fromObject(data, jsonConfig);
		WebGuiResponseUtils.sendAJAXBean(response, new AJAXTransferBean(getMessageByKey(request,"message.collateral.flexEE.successful","ajax"), result));
        
		return null;
	}
	
	private String getMessageByKey(HttpServletRequest request,String key,String param) {
//		return getResources(request).getMessage(key, param);
		return getResources(request).getMessage(getLocale(request), key, param);
	}

}
